//
//  LFHomeViewCell.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/25.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

import SnapKit

class LFHomeViewCell: UITableViewCell {
    
    // MARK: - 属性
    /// cell的BottomView的顶部约束
    var bottomViewBottomCons: Constraint?
    
    //传值给originalView
    var status : LFStatus? {
        didSet {
            
            originalView.status = status
            
             self.bottomViewBottomCons?.uninstall()
//            print("status?.retweeted_status\(status?.retweeted_status)")
            if let retweetedStatus = status?.retweeted_status {
                
                //传值给retweetView 的retweeted_status
                retweetView.retweetedStatus = retweetedStatus
                retweetView.hidden = false
                threeBtn.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else {
                retweetView.hidden = true
                threeBtn.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    

      //MARK: -- 初始化
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        
        //设置cell的背景颜色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    }
    
      //MARK: --  UI布局
    private func prepareUI() {
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(threeBtn)
        
        //原生View约束
        originalView.snp_makeConstraints { (make) -> Void in
            //设置出边距
           make.top.equalTo(contentView).offset(10)
           make.leading.right.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.leading.equalTo(contentView)
        }
        
        //三个按钮
        threeBtn.snp_makeConstraints { (make) -> Void in
            self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
  
        //contentView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(threeBtn)
        }
        
        //优化cell界面
        //1.异步绘制
        self.layer.drawsAsynchronously = true
        
        //2.栅格化/光栅化
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
        
    }
    
    //MARK: -- 懒加载
    private lazy var originalView : LFOriginalView = LFOriginalView()
    
    //三个按钮
    private lazy var threeBtn : LFButtonView = LFButtonView()
    
    //转发微博
    private lazy var retweetView : LFRetweetView = LFRetweetView()
  

}
